"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const defines_1 = require("../lib/defines");
const enums_1 = require("../utils/enums");
const { record, stopRecord } = defines_1.RequestMethod;
function recording(app) {
    const { socket, rooms, clientUtils } = app.locals;
    const { userId, username } = socket.handshake.query;
    socket.on(record, async function __recording({ room, member, url }, cb) {
        const __room = rooms[room.classId];
        if (!__room)
            return cb && cb({ status: 'error', message: 'room not find!' });
        const __member = __room.members[member.userId];
        if (!__member)
            return cb && cb({ status: 'error', message: 'member not find!' });
        const pipeline = __room.members[userId].pipeline;
        if (!pipeline)
            cb && cb({ status: 'error', message: 'pipeline not find!' });
        const endpoint = __member.getEndpoint(userId);
        if (!endpoint)
            cb && cb({ status: 'error', message: `user(userId:${userId}; username:${username}) is not speaker!` });
        if (__room.chatMode === enums_1.ChatModeEnums.BROADCAST) {
            return cb && cb({ status: 'error', message: `广播不允许录制!` });
        }
        const __rul = url || `file:///tmp/kurento_record/${Date.now()}`;
        const recorder = await clientUtils.createRecordEndpoint(pipeline, __rul);
        await clientUtils.connect(endpoint, recorder);
        __member.recorder = recorder;
        __member.player.on('EndOfStream', async (data) => {
            console.log('EndOfStream', data);
            console.log('停止录制');
            await recorder.stop();
            console.log('释放录制');
            await recorder.release();
            console.log('释放播放器');
            await __member.player.release();
        });
        recorder.record(function (error) {
            if (error)
                return cb({ status: 'error', message: error });
            cb && cb({ status: 'ok', data: url });
        });
    });
    socket.on(stopRecord, async function __recordStop({ room, member, url }, cb) {
        const __room = rooms[room.classId];
        if (!__room)
            return cb && cb({ status: 'error', message: 'room not find!' });
        const __member = __room.members[member.userId];
        if (!__member)
            return cb && cb({ status: 'error', message: 'member not find!' });
        if (__room.chatMode === enums_1.ChatModeEnums.BROADCAST) { }
        __member.player.getUri(function (error, url) {
            if (error)
                return cb({ status: 'error', message: error });
            return __member.recorder.release(function (error) {
                if (error)
                    return cb({ status: 'error', message: error });
                return __member.player.release(function (error) {
                    if (error)
                        return cb({ status: 'error', message: error });
                    return cb && cb({ status: 'ok', message: url });
                });
            });
        });
    });
}
exports.default = recording;
